package com.ruoyi.project.system.presentation.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.presentation.domain.DataPresentation;
import com.ruoyi.project.system.presentation.service.IDataPresentationService;
import com.ruoyi.project.system.state.service.IDataStateService;
import com.ruoyi.project.system.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 数据展示Controller
 * 
 * @author ruoyi
 * @date 2022-02-20
 */
@Component("hrTask")
@Controller
@RequestMapping("/system/presentation")
public class DataPresentationController extends BaseController
{
    private String prefix = "system/presentation";

    @Autowired
    private IDataPresentationService dataPresentationService;

    @Autowired
    private IDataStateService dataStateService;

    @Resource
    private ObjectMapper objectMapper;

//    @RequiresPermissions("system:presentation:view")
    @GetMapping()
    public String presentation()
    {
        return prefix + "/presentation";
    }




    /**
     * 查询数据展示列表
     */
//    @RequiresPermissions("system:presentation:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DataPresentation dataPresentation,Integer state,Integer classifyState)
    {

        // 获取当前的用户信息
        User currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户名称
        String loginUser = currentUser.getLoginName();
        if (!loginUser.equals("admin")) {
            dataPresentation.setUserName(loginUser);
        }
        List<DataPresentation> list = dataPresentationService.selectDataPresentationList(dataPresentation,state,classifyState);
        return getDataTable(list);
    }


//    @PostMapping("/selectList")
//    @ResponseBody
//    public TableDataInfo selectList(DataPresentation dataPresentation,Integer state)
//    {
//
//        // 获取当前的用户信息
//        User currentUser = ShiroUtils.getSysUser();
//        // 获取当前的用户名称
//        String loginUser = currentUser.getLoginName();
//        if (!loginUser.equals("admin")) {
//            dataPresentation.setUserName(loginUser);
//        }
//        List<DataPresentation> list = dataPresentationService.selectList(dataPresentation,state,);
//        return getDataTable(list);
//    }




    @PostMapping("/dateList")
    @ResponseBody
    public TableDataInfo dateList(String state)
    {
        startPage();
        if (state=="1"){
            List<DataPresentation> list = dataPresentationService.selectDataPresentationByWeek();
            return getDataTable(list);
        }
        if (state=="2"){
            List<DataPresentation> list = dataPresentationService.selectDataPresentationByMonth();
            return getDataTable(list);
        }
        if (state=="3"){
            List<DataPresentation> list = dataPresentationService.selectDataPresentationBySeason();
            return getDataTable(list);
        }
        if (state=="4"){
            List<DataPresentation> list = dataPresentationService.selectDataPresentationByYear();
            return getDataTable(list);
        }else
        {
            List<DataPresentation> list = dataPresentationService.selectDataPresentationByYearMonthDay();
            return getDataTable(list);

        }
    }




    /**
     * 导出数据展示列表
     */
//    @RequiresPermissions("system:presentation:export")
    @Log(title = "数据展示", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DataPresentation dataPresentation,Integer state,Integer classifyState)
    {
        List<DataPresentation> list = dataPresentationService.selectDataPresentationList(dataPresentation,state,classifyState);
        ExcelUtil<DataPresentation> util = new ExcelUtil<DataPresentation>(DataPresentation.class);
        return util.exportExcel(list, "数据展示数据");
    }

    /**
     * 新增数据展示
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存数据展示
     */
//    @RequiresPermissions("system:presentation:add")

//    @Log(title = "数据展示", businessType = BusinessType.INSERT)
//    @PostMapping("/add")
//    @ResponseBody
//    public void addSave()
//    {
//
//        //Calendar calendar = Calendar.getInstance();
//        //获取当时间前一天
//        Calendar calendar1 = Calendar.getInstance();
//        calendar1.add(Calendar.DATE, -1);
//
//        //获取前一天是本月的第几天
//        Long day = (long)calendar1.get(Calendar.DAY_OF_MONTH);
//        //获取前一天是本年的第几周
//        Long week = (long)calendar1.get(Calendar.WEEK_OF_YEAR);
//        //获取前一天是本年的第几个月
//        Long month = (long)calendar1.get(Calendar.MONTH)+1;
//        //获取前一天是本年的第几季度
//        long quarter ;
//        if (month >= 1 && month <= 3) {
//            quarter = 1;
//        } else if (month >= 4 && month <= 6) {
//            quarter = 2;
//        } else if (month >= 7 && month <= 9) {
//            quarter = 3;
//        } else {
//            quarter = 4;
//        }
//        //获取前一天是第哪一年
//        Long year = (long)calendar1.get(Calendar.YEAR);
//        //获取本周第几天
//        Integer dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK);
//        //获取当前日期
////        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
////        Date date=new Date();
////        String formatDate = df.format(date);
//
//        //将当前时间转为时间戳
//        long  endTime= Calendar.getInstance().getTimeInMillis()/1000;
//        //将前一天时间转为时间戳
//        Date time = calendar1.getTime();
//        long beginTime = time.getTime()/1000;
//
//
//        List<DataPresentation> dataPresentations = dataPresentationService.selectDataPresentationByTimeHorizon(beginTime,endTime);
//        for (int i = 0; i < dataPresentations.size(); i++) {
//            DataPresentation dataPresentation = new DataPresentation();
//
//            DataPresentation dataPresentation1 = dataPresentations.get(i);
//            Long applicationNumber = dataPresentation1.getApplicationNumber();
//            Long equipmentId = dataPresentation1.getEquipmentId();
//            Long throughNumber = dataPresentation1.getThroughNumber();
//            String userName = dataPresentation1.getUserName();
//            String verbalTrick = dataPresentation1.getVerbalTrick();
//            String wechatId = dataPresentation1.getWechatId();
//
//            dataPresentation.setApplicationNumber(applicationNumber);
//            dataPresentation.setEquipmentId(equipmentId);
//            dataPresentation.setThroughNumber(throughNumber);
//            dataPresentation.setUserName(userName);
//            dataPresentation.setVerbalTrick(verbalTrick);
//            dataPresentation.setWechatId(wechatId);
//            dataPresentation.setDay(day);
//            dataPresentation.setWeek(week);
//            dataPresentation.setMonth(month);
//            dataPresentation.setYear(year);
//            dataPresentation.setSeason(quarter);
//            dataPresentation.setDayOfWeek(dayOfWeek);
//            dataPresentationService.insertDataPresentation(dataPresentation);
//        }
//    }






    /**
     * 修改保存数据展示
     */
//    @RequiresPermissions("system:presentation:edit")
    @Log(title = "数据展示", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DataPresentation dataPresentation)
    {
        return toAjax(dataPresentationService.updateDataPresentation(dataPresentation));
    }

    /**
     * 删除数据展示
     */
//    @RequiresPermissions("system:presentation:remove")
    @Log(title = "数据展示", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dataPresentationService.deleteDataPresentationByIds(ids));
    }
}
